var interfaceFieldInfos = new Array();
interfaceInfo["interfaceFieldInfos"]=interfaceFieldInfos;

function createInterfaceFieldInfo(){
	var pInterfaceField = $("#interfaceFieldInfo>option:selected").data("data");
	var interfaceFieldInfo={
			interfaceFieldId:Math.uuid(),
			interfaceFieldName:$("#interfaceFieldName").val(),
			interfaceFieldType:$("#interfaceFieldType").val(),
			interfaceFieldPosition:$("#interfaceFieldPosition").val(),
			interfaceFieldIsNull:$("#interfaceFieldIsNull").get(0).checked,
			interfaceFieldExplain:$("#interfaceFieldExplain").val(),
			interfaceFieldInfo:pInterfaceField,
			depth:pInterfaceField == undefined ? 0 : 1
	}
	if(pInterfaceField == undefined){
		interfaceFieldInfos.push(interfaceFieldInfo);
	}else{
		pInterfaceField["interfaceFieldInfos"].push(interfaceFieldInfo);
	}
	showFidld();
}

function getInterfaceField(interfaceId){
	$.ajax({
		type : 'POST',
		url : "../interfaceField/getInterfaceField.do",
		async:false,
		data : {
			interfaceId:interfaceId
		},
		dataType : "JSON",
		success : function(data){
			interfaceFieldInfos = data["data"];
			$(interfaceFieldInfos).each(function(i,o){
				o["depth"]=0;
				getInterfaceFieldInfo(o);
			});
		}
	});
}
function getInterfaceFieldInfo(interfaceFieldInfo){
	$.ajax({
		type : 'POST',
		url : "../interfaceField/getInterfaceField.do",
		async:false,
		data : {
			interfaceFieldId:interfaceFieldInfo["interfaceFieldId"]
		},
		dataType : "JSON",
		success : function(data){
			var interfaceFieldInfos = data["data"];
			interfaceFieldInfo["interfaceFieldInfos"]=interfaceFieldInfos;
			$(interfaceFieldInfos).each(function(i,o){
				o["depth"]=1;
				getInterfaceFieldInfo(o);
			});
		}
	});
}

function showSelect(){

	var option=$("<option>接口</option>");
	
	var pinterfaceFieldInfo = $("#field_item").find("select[id^='interfaceFieldInfo_']");
	pinterfaceFieldInfo.empty();
	pinterfaceFieldInfo.append(option);
	showSelects(interfaceFieldInfos,pinterfaceFieldInfo);
	
	var interfaceFieldInfo = $("#interfaceFieldInfo");
	interfaceFieldInfo.empty();
	interfaceFieldInfo.append(option);
	showSelects(interfaceFieldInfos,interfaceFieldInfo);
	pinterfaceFieldInfo.each(function(i,o){
		$(o).find("option[value='"+$(o).data("data")["interfaceFieldId"]+"']").remove();
		deleteOption($(o),$(o).data("data")["interfaceFieldInfos"]);
		if($(o).data("data")["interfaceFieldInfo"] != undefined){
			var pInterfaceFieldId=$(o).data("data")["interfaceFieldInfo"]["interfaceFieldId"];
			$(o).val(pInterfaceFieldId);			
		}
	});
}
function deleteOption(select,interfaceFieldInfos){
	$(interfaceFieldInfos).each(function(i,o){
		select.find("option[value='"+o["interfaceFieldId"]+"']").remove();
		if(o["interfaceFieldInfos"] != undefined ){
			deleteOption(select, o["interfaceFieldInfos"]);
		}
	});
}
function showSelects(item,pinterfaceFieldInfo){
	$(item).each(function(i,o){
		var option=$("<option value=\""+o["interfaceFieldId"]+"\">"+o["interfaceFieldName"]+"</option>");
		option.data("data",o);
		pinterfaceFieldInfo.append(option);
		if(o["interfaceFieldInfos"] != undefined){
			showSelects(o["interfaceFieldInfos"],pinterfaceFieldInfo);
		}
	});
}

function showFidld(){
	$("#field_item").empty();
	showFidlds(interfaceFieldInfos);
	showSelect();
}
function showFidlds(item){
	$(item).each(function(i,o){
		createFidldTable(o);
		if(o["interfaceFieldInfos"] != undefined){
			showFidlds(o["interfaceFieldInfos"]);
		}
	});
}
function createFidldTable(interfaceFieldInfo){
	var tbody;
	if(interfaceFieldInfo["interfaceFieldInfo"] == null){
		tbody=$("#field_item");
	}else{
		tbody=$("#tbody_interfaceFieldInfos_"+interfaceFieldInfo["interfaceFieldInfo"]["interfaceFieldId"]);
	}
	if(tbody.length == 0){
		var tr_interfaceFieldInfos=$($("#tableTemplates").html());
		tr_interfaceFieldInfos.attr("id","tr_interfaceFieldInfos_"+interfaceFieldInfo["interfaceFieldInfo"]["interfaceFieldId"]);

		var td_interfaceFieldInfos_colspan = tr_interfaceFieldInfos.find("#td_interfaceFieldInfos_colspan_Templates");
		td_interfaceFieldInfos_colspan.attr("id","td_interfaceFieldInfos_colspan_"+interfaceFieldInfo["interfaceFieldInfo"]["interfaceFieldId"]);
		td_interfaceFieldInfos_colspan.attr("colspan",7-interfaceFieldInfo["depth"]);
		
		tbody = tr_interfaceFieldInfos.find("#tbody_interfaceFieldInfos_templates");
		tbody.attr("id","tbody_interfaceFieldInfos_"+interfaceFieldInfo["interfaceFieldInfo"]["interfaceFieldId"]);
		
		$("#tr_"+interfaceFieldInfo["interfaceFieldInfo"]["interfaceFieldId"]).after(tr_interfaceFieldInfos);
		
	}
	var tr=$($("#fieldTemplates").html());
	tr.attr("id","tr_"+interfaceFieldInfo["interfaceFieldId"]);
	var interfaceFieldName = tr.find("#interfaceFieldName_templates");
	interfaceFieldName.attr("id","interfaceFieldName_"+interfaceFieldInfo["interfaceFieldId"]);
	interfaceFieldName.attr("name","interfaceFieldName_"+interfaceFieldInfo["interfaceFieldId"]);
	interfaceFieldName.data("data",interfaceFieldInfo);
	interfaceFieldName.val(interfaceFieldInfo["interfaceFieldName"]);
	
	var interfaceFieldType = tr.find("#interfaceFieldType_templates");
	interfaceFieldType.attr("id","interfaceFieldType_"+interfaceFieldInfo["interfaceFieldId"]);
	interfaceFieldType.attr("name","interfaceFieldType_"+interfaceFieldInfo["interfaceFieldId"]);
	interfaceFieldType.data("data",interfaceFieldInfo);
	interfaceFieldType.val(interfaceFieldInfo["interfaceFieldType"]);
	
	var interfaceFieldPosition = tr.find("#interfaceFieldPosition_templates");
	interfaceFieldPosition.attr("id","interfaceFieldPosition_"+interfaceFieldInfo["interfaceFieldId"]);
	interfaceFieldPosition.attr("name","interfaceFieldPosition_"+interfaceFieldInfo["interfaceFieldId"]);
	interfaceFieldPosition.data("data",interfaceFieldInfo);
	interfaceFieldPosition.val(interfaceFieldInfo["interfaceFieldPosition"]);
	
	var interfaceFieldIsNull = tr.find("#interfaceFieldIsNull_templates");
	interfaceFieldIsNull.attr("id","interfaceFieldIsNull_"+interfaceFieldInfo["interfaceFieldId"]);
	interfaceFieldIsNull.attr("name","interfaceFieldIsNull_"+interfaceFieldInfo["interfaceFieldId"]);
	interfaceFieldIsNull.attr("checked",interfaceFieldInfo["interfaceFieldIsNull"] ? "checked" : undefined);
	interfaceFieldIsNull.data("data",interfaceFieldInfo);
	
	var interfaceFieldExplain = tr.find("#interfaceFieldExplain_templates");
	interfaceFieldExplain.attr("id","interfaceFieldExplain_"+interfaceFieldInfo["interfaceFieldId"]);
	interfaceFieldExplain.attr("name","interfaceFieldExplain_"+interfaceFieldInfo["interfaceFieldId"]);
	interfaceFieldExplain.data("data",interfaceFieldInfo);
	interfaceFieldExplain.val(interfaceFieldInfo["interfaceFieldExplain"]);

	var pinterfaceFieldInfo = tr.find("#interfaceFieldInfo_templates");
	pinterfaceFieldInfo.attr("id","interfaceFieldInfo_"+interfaceFieldInfo["interfaceFieldId"]);
	pinterfaceFieldInfo.attr("name","interfaceFieldInfo_"+interfaceFieldInfo["interfaceFieldId"]);
	pinterfaceFieldInfo.data("data",interfaceFieldInfo);
	if(interfaceFieldInfo["interfaceFieldInfo"] != undefined ){
		pinterfaceFieldInfo.val(interfaceFieldInfo["interfaceFieldInfo"]["interfaceFieldId"]);	
	}
	tbody.append(tr);
}